package cn.wjh.business.controller;

import cn.wjh.business.domain.Comment;
import cn.wjh.business.service.ICommentService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 评论Controller
 *
 * @author jiraWu
 * @date 2023-10-26
 */
@RestController
@RequestMapping("/business/comment")
public class CommentController extends BaseController {
	@Resource
	private ICommentService commentService;

	/**
	 * 查询评论列表
	 */
//	@PreAuthorize("@ss.hasPermi('business:comment:list')")
	@GetMapping("/list")
	@Anonymous
	public TableDataInfo list(Comment comment) {
		startPage();
		List<Comment> list = commentService.selectCommentList(comment);
		return getDataTable(list);
	}

	@GetMapping("/first")
	@Anonymous
	public TableDataInfo first(Comment comment) {
		startPage();
		List<Comment> list = commentService.selectFirstCommentList(comment);
		return getDataTable(list);
	}

	@GetMapping("/child/{commentId}")
	@Anonymous
	public TableDataInfo child(@PathVariable Long commentId) {
		startPage();
		List<Comment> list = commentService.selectChildByRootId(commentId);
		return getDataTable(list);
	}

	/*获取我的评论*/
	@GetMapping("/my")
	public TableDataInfo myList(Comment comment) {
		startPage();
		List<Comment> list = commentService.myComment(comment);
		return getDataTable(list);
	}

	/*将评论设置为已读*/
	@GetMapping("/read/{commentIds}")
	public AjaxResult readComment(@PathVariable List<Long> commentIds) {
		return toAjax(commentService.readComment(commentIds));
	}

	/*获取已读和未读消息的数量*/
	@GetMapping("/readAndNotReadCount")
	public AjaxResult readAndNotReadCount(Comment comment) {
		return success(commentService.readAndNotReadCount(comment));
	}

	/*获取评论我的*/
	@GetMapping("/commentMy")
	public TableDataInfo commentMy(Comment comment) {
		startPage();
		List<Comment> list = commentService.commentMy(comment);
		return getDataTable(list);
	}


	/**
	 * 导出评论列表
	 */
//	@PreAuthorize("@ss.hasPermi('business:comment:export')")
	@Log(title = "评论", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, Comment comment) {
		List<Comment> list = commentService.selectCommentList(comment);
		ExcelUtil<Comment> util = new ExcelUtil<Comment>(Comment.class);
		util.exportExcel(response, list, "评论数据");
	}

	/**
	 * 获取评论详细信息
	 */
//	@PreAuthorize("@ss.hasPermi('business:comment:query')")
	@GetMapping(value = "/{commentId}")
	public AjaxResult getInfo(@PathVariable("commentId") Long commentId) {
		return success(commentService.selectCommentByCommentId(commentId));
	}

	/**
	 * 评论(1级)
	 */
//	@PreAuthorize("@ss.hasPermi('business:comment:add')")
	@Log(title = "评论", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody Comment comment) {
		AjaxResult result = toAjax(commentService.insertComment(comment));
		result.put(AjaxResult.DATA_TAG, comment.getCommentId());
		return result;
	}

	/**
	 * 评论(2级) 相当于回复别人的评论
	 */
	@PostMapping("/reply")
	public AjaxResult sendReply(@RequestBody Comment comment) {
		AjaxResult result = toAjax(commentService.sendReply(comment));
		result.put(AjaxResult.DATA_TAG, comment.getRootId());
		return result;
	}


	/**
	 * 修改评论
	 */
	@PreAuthorize("@ss.hasPermi('business:comment:edit')")
	@Log(title = "评论", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody Comment comment) {
		return toAjax(commentService.updateComment(comment));
	}

	/**
	 * 删除评论
	 */
//	@PreAuthorize("@ss.hasPermi('business:comment:remove')")
	@Log(title = "评论", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentIds}")
	public AjaxResult remove(@PathVariable Long[] commentIds) {
		return toAjax(commentService.deleteCommentByCommentIds(commentIds));
	}


}
